var AdvancePayment = function () {
  var template = /* html */ `
    <div class="advance-payment" v-loading.fullscreen.lock="loading">
      <div style="margin-bottom:10px">
        <el-button size="medium" type="primary" @click="toPayment(1)">
          {{type==='out'?'收款':'付款'}}
        </el-button>
        <el-button size="medium" type="primary" :disabled="!hasApproval" @click="toPayment(2)">审核过账</el-button>
      </div>
      <div class="advance-payment-table-div">
        <el-table fit stripe border highlight-current-row :data="paymentList" style="width: 100%" height="parent" @current-change="currentChange">
          <el-table-column label="预收付款金额" prop="sumMoney" min-width="120px" align="center"></el-table-column>
          <el-table-column label="预收付款方" prop="receviePaymentUserName" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="用途" width="100px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('financeMgr.advancePayment.source.'+scope.row.source)}}</span>
            </template>
          </el-table-column>
          <el-table-column label="用途描述" prop="sourceStr" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="合同编号" prop="advanceNo" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="预收付款日期" width="100px" align="center">
            <template slot-scope="scope">
              <span>{{moment(scope.row.paymentDay).format('YYYY-MM-DD')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="银行卡号" prop="relationBank" min-width="160px" header-align="center"></el-table-column>
          <el-table-column label="所属银行" prop="bank" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="状态" min-width="80px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('financeMgr.advancePayment.status.'+scope.row.status)}}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      paymentForm: {
        type: Object,
        required: true
      },
      hasApproval: {
        type: Boolean,
        default: false
      },
      type: {
        type: String,
        default: 'material'
      }
    },

    data: function () {
      return {
        loading: false,
        paymentList: [],
        selectPayment: null
      };
    },

    created: function () {
      this.setPaymentList();
    },

    methods: {
      setPaymentList: function () {
        var type = this.type;
        var customerName = this.paymentForm.customerName;

        this.loading = true;
        var self = this;
        ajaxRequest('finance/getAdvancePayRecord', {
          type: type === 'out' ? 1 : 2,
          receviePaymentUserName: customerName,
          status: 1,
          isUsed: 0
        }).then(function (res) {
          var paymentList = res.pageData.returnData;
          self.paymentList = paymentList;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      currentChange: function (payment) {
        this.selectPayment = payment;
      },

      toPayment: function (payType) {
        var payment = this.selectPayment;
        if (payment === null) {
          var msg = this.type === 'out' ? '请选择预收款' : '请选择预付款';
          this.$alert(msg, '信息');
          return;
        }

        var paymentForm = this.paymentForm;
        var sumMoney = paymentForm.getSumPayPrice();
        if (sumMoney !== payment.sumMoney) {
          var msg = this.type === 'out' ? '预收款与实际收款不一致' : '预付款与实际付款不一致';
          this.$alert(msg, '信息');
          return;
        }

        paymentForm.relationBank = payment.relationBank;
        var postData = paymentForm.getObject(payType,this.type);
        postData.advancePaymenteceiveId = payment.id;

        this.loading = true;
        var self = this;
        ajaxRequest('warehouseInSumMoney/saveReceiptPaymentRecord', {
          jsonObject: JSON.stringify(postData)
        }).then(function () {
          var msg = '';
          if (self.type === 'out') {
            msg = payType === 1 ? '成功收款' : '成功收款并审核通过';
          } else {
            msg = payType === 1 ? '成功付款' : '成功付款并审核通过';
          }
          self.$alert(msg, '信息', {
            callback: function () {
              self.$emit('pay-success');
            }
          });
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
